package cn.ffcs.uec.common.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import cn.ffcs.uec.common.dao.BaseDaoImpl;
import cn.ffcs.uec.common.dao.CustomFilesDao;
import cn.ffcs.uec.common.model.CustomFiles;
import cn.ffcs.uec.common.vo.SysDictVo;
import cn.ffcs.uec.common.vo.UecBdDefdoc;
import cn.ffcs.uec.common.vo.UecBdPsncl;
import cn.ffcs.uec.common.vo.UecWaPrmlv;
import cn.ffcs.uec.common.vo.UecWaSeclv;
import cn.ffcs.uec.post.vo.BasePostVo;
import cn.ffcs.uom.common.util.StrUtil;

@Repository("customFilesDao")
public class CustomFilesDaoImpl extends BaseDaoImpl implements CustomFilesDao {

	@Override
	public CustomFiles findCustomFilesByCustomFiles(CustomFiles customFiles) {
		if (null != customFiles) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_BD_DEFDOC T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			if (null != customFiles.getPkDefdoc()) {
				sb.append(" AND T.PK_DEFDOC = ? ");
				params.add(customFiles.getPkDefdoc());
			}
			if (null != customFiles.getPkOrg()) {
				sb.append(" AND T.PK_ORG = ? ");
				params.add(customFiles.getPkOrg());
			}
			if (null != customFiles.getPkGroup()) {
				sb.append(" AND T.PK_GROUP = ? ");
				params.add(customFiles.getPkGroup());
			}
			List<CustomFiles> list = super.jdbcFindList(sb.toString(), params, CustomFiles.class);
			if (list.size() > 0 && list != null) {
				return list.get(0);
			}
		}
		return null;
	}
	
	@Override
	public String findCustomFilesByPkdefdoc(String pkdefdoc) {
		if(!StrUtil.isEmpty(pkdefdoc)) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_BD_DEFDOC T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.PK_DEFDOC = ? ");
			params.add(pkdefdoc);
			List<CustomFiles> list = super.jdbcFindList(sb.toString(), params, CustomFiles.class);
			if (list.size() > 0 && list != null) {
				return list.get(0).getName();
			}
		}
		return "";
	}
	
	@Override
	public List<UecBdDefdoc> findCustomFilesByPkdefdocList(String pkdefdocList) {
		if(!StrUtil.isEmpty(pkdefdocList)) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_BD_DEFDOC T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.PK_DEFDOCLIST = ? ");
			sb.append(" AND T.DR = '0' ");
			sb.append(" order by T.CODE asc");
			params.add(pkdefdocList);
			List<UecBdDefdoc> list = super.jdbcFindList(sb.toString(), params, UecBdDefdoc.class);
			return list;
		}
		return null;
	}

	@Override
	public List<UecBdPsncl> findUecUecBdPsnclByUecBdPsnclList(String pkOrg) {
		if(!StrUtil.isEmpty(pkOrg)) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_BD_PSNCL T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.PK_ORG = ? ");
			params.add(pkOrg);
			List<UecBdPsncl> list = super.jdbcFindList(sb.toString(), params, UecBdPsncl.class);
			return list;
		}
		return null;
	}

	@Override
	public UecBdPsncl findUecUecBdPsnclByUecBdPsncl(UecBdPsncl uecBdPsncl) {
		if (null != uecBdPsncl) {
			if (!StrUtil.isNullOrEmpty(uecBdPsncl.getPkPsncl())) {
				StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_BD_PSNCL T WHERE T.PK_PSNCL = ? ");
				List<Object> params = new ArrayList<Object>();
				params.add(uecBdPsncl.getPkPsncl());
				List<UecBdPsncl> list = super.jdbcFindList(sb.toString(), params, UecBdPsncl.class);
				if (list.size() > 0 && list != null) {
					return list.get(0);
				}
			}
		}
		return null;
	}
	
	@Override
	public UecWaPrmlv findObjectByUecWaPrmlv(UecWaPrmlv uecWaPrmlv) {
		if(null != uecWaPrmlv) {
			StringBuilder sql = new StringBuilder(" SELECT * FROM  UEC_WA_PRMLV T WHERE 1=1  ");
			List<Object> params = new ArrayList<Object>();
			if (!StrUtil.isNullOrEmpty(uecWaPrmlv.getPkWaPrmlv())) {
				sql.append(" AND T.PK_WA_PRMLV = ? ");
				params.add(uecWaPrmlv.getPkWaPrmlv());
			}
			if (!StrUtil.isNullOrEmpty(uecWaPrmlv.getLevelName())) {
				sql.append(" AND T.LEVELNAME = ? ");
				params.add(uecWaPrmlv.getLevelName());
			}
			if (!StrUtil.isNullOrEmpty(uecWaPrmlv.getPkWaGrd())) {
				sql.append(" AND T.PK_WA_GRD = ?");
				params.add(uecWaPrmlv.getPkWaGrd());
			}
			List<UecWaPrmlv> list = super.jdbcFindList(sql.toString(), params, UecWaPrmlv.class);
			if (list != null && list.size() > 0) {
				return list.get(0);
				}
		}
		return null;
	}

	@Override
	public UecWaSeclv findObjectByUecWaSeclv(UecWaSeclv uecWaSeclv) {
		if(null != uecWaSeclv) {
			StringBuilder sql = new StringBuilder(" SELECT * FROM  UEC_WA_SECLV T WHERE 1=1  ");
			List<Object> params = new ArrayList<Object>();
			if (!StrUtil.isNullOrEmpty(uecWaSeclv.getPkWaSeclv())) {
				sql.append(" AND T.PK_WA_SECLV = ? ");
				params.add(uecWaSeclv.getPkWaSeclv());
			}
			if (!StrUtil.isNullOrEmpty(uecWaSeclv.getLevelName())) {
				sql.append(" AND T.LEVELNAME = ? ");
				params.add(uecWaSeclv.getLevelName());
			}
			if (!StrUtil.isNullOrEmpty(uecWaSeclv.getPkWaGrd())) {
				sql.append(" AND T.PK_WA_GRD = ?");
				params.add(uecWaSeclv.getPkWaGrd());
			}
			List<UecWaSeclv> list = super.jdbcFindList(sql.toString(), params, UecWaSeclv.class);
			if (list != null && list.size() > 0) {
				return list.get(0);
				}
		}
		return null;
	
	}

	@Override
	public Boolean getActivitySwitchBySysDictVo(String type) {
		if(!StrUtil.isEmpty(type)) {
			StringBuilder sql = new StringBuilder(" SELECT * FROM  SYS_DICT T WHERE T.TYPE = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(type);
			SysDictVo sysDictVo = super.jdbcFindObject(sql.toString(), params, SysDictVo.class);
			if(sysDictVo != null) {
				if("true".equals(sysDictVo.getValue())) {
					return true;
				}else {
					return false;
				}
			}
		}
		return false;
	}

	@Override
	public String getBasePostNameByCode(String code) {
		if(!StrUtil.isEmpty(code)) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM BASE_POST WHERE CODE = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(code);
			List<BasePostVo> list = super.jdbcFindList(sb.toString(), params, BasePostVo.class);
			if (list.size() > 0 && list != null) {
				return list.get(0).getPostname();
			}
		}
		return "";
	}
/**
 *    在表sys_dict中根据类型type  查询对应value   
 */
	@Override
	public String getSysDictVo(String type) {
		String returnValue="";
		if(!StrUtil.isEmpty(type)) {
			StringBuilder sql = new StringBuilder(" SELECT * FROM  SYS_DICT T WHERE T.TYPE = ? ");
			List<Object> params = new ArrayList<Object>();
			params.add(type);
			SysDictVo sysDictVo = super.jdbcFindObject(sql.toString(), params, SysDictVo.class);
			if(sysDictVo != null) {
				returnValue= sysDictVo.getValue();
			}
		}
		return returnValue;
	}


}
